找传奇、传世资源到传世资源站!
API 正文

asp.net webapi 支持文件下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Net.Http.Headers;using System.Web.Http;namespace DownloadFileFromWebApi.Controllers{ [RoutePrefix("download")] public class DownloadController : ApiController {  [Route("get_demo_file")]  public HttpResponseMessage GetFileFromWebApi()  {   try   {    var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/download/EditPlus64_xp85.com.zip");    var stream = new FileStream(FilePath, FileMode.Open);    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);    response.Content = new StreamContent(stream);    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {     FileName="Wep Api Demo File.zip"    };    return response;   }   catch   {    return new HttpResponseMessage(HttpStatusCode.NoContent);   }  } }}        

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复